<template>
	<div class="layout-padding-auto layout-padding-view">
		<!-- 搜索 -->
		<el-form :model="state.queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-form-item label="属性值">
				<el-input v-model="state.queryParams.name" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="initPage"
					><el-icon> <ele-Search /> </el-icon>搜索</el-button
				>
				<el-button @click="resetQuery"
					><el-icon> <ele-Refresh /> </el-icon>重置</el-button
				>
			</el-form-item>
		</el-form>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div>
				<el-button type="primary" v-auth="'product:goodsspecsvalue:add'" @click="add"
					><el-icon> <ele-Plus /> </el-icon>新增</el-button
				>
			</div>
			<right-toolbar :searchBtn="true" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<Form ref="refForm" @initPage="initPage"></Form>
		<!-- 列表 -->
		<el-table v-loading="loading" :data="state.tableData" border>
			<el-table-column prop="name" label="规格值" />
			<el-table-column prop="createTime" label="创建时间" />
			<el-table-column label="操作" width="300" align="center">
				<template #default="scope">
					<el-button link type="primary" v-auth="'product:goodsspecsvalue:edit'" @click="edit(scope.row)"
						><el-icon> <ele-Edit /> </el-icon>修改</el-button
					>
					<el-button link type="danger" icon="delete" v-auth="'product:goodsspecsvalue:del'" @click="del(scope.row.id)"
						><el-icon> <ele-Delete /> </el-icon>删除</el-button
					>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页 -->
		<pagination
			v-model:total="state.page.total"
			v-model:currentPage="state.page.currentPage"
			v-model:pageSize="state.page.pageSize"
			@changePage="initPage"
		></pagination>
	</div>
</template>
<script lang="ts" setup>
import { defineAsyncComponent, ref, reactive } from 'vue';
import { getPage, delObj } from '/@/api/product/goodsspecsvalue';
import { ElMessageBox, ElMessage } from 'element-plus';
const Form = defineAsyncComponent(() => import('./form.vue'));

const props = defineProps({
	specsId: {
		type: String,
		default: '',
	},
	value: [String, Array],
});
const state = reactive({
	queryParams: { name: '' },
	page: {
		total: 0,
		currentPage: 1,
		pageSize: 10,
		descs: 'create_time',
	},
	tableData: [],
});
const showSearch = ref(true);
const loading = ref(false);
const refForm = ref();

const initPage = async () => {
	if (props.specsId) {
		loading.value = true;
		let params = {
			current: state.page.currentPage,
			size: state.page.pageSize,
			specsId: props.specsId,
			descs: state.page.descs,
		};
		await getPage(Object.assign(params, state.queryParams))
			.then((response) => {
				state.tableData = response.data.data.records;
				state.page.total = response.data.data.total;
				loading.value = false;
			})
			.catch(() => {
				loading.value = false;
			});
	}
};
/**
 * 重置搜索表单
 */
const resetQuery = () => {
	state.queryParams = { name: '' };
};
/**
 * 新增按钮
 */
const add = () => {
	refForm.value.initForm(null, props.specsId);
};
/**
 * 修改按钮
 */
const edit = (row: any) => {
	refForm.value.initForm(row, props.specsId);
};
/**
 * 删除按钮
 */
const del = (id: string) => {
	ElMessageBox.confirm('此操作将删除该属性值，是否继续?', '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		delObj(id)
			.then((response) => {
				ElMessage.success('删除成功');
				initPage();
			})
			.catch(() => {});
	});
};
defineExpose({
	initPage,
});
</script>
